#!/usr/bin/make -f

include /usr/share/dpatch/dpatch.make

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS = unoptimized
else
	CFLAGS = optimized
endif

PUGS_BUILD_OPTS = $(DEB_BUILD_OPTIONS)

configure: configure-stamp
configure-stamp: patch-stamp
	dh_testdir
	PUGS_EMBED=perl5,parrot perl Makefile.PL INSTALLDIRS=vendor PREFIX=/usr
	touch configure-stamp

build: build-arch build-indep

build-arch: build-arch-stamp
build-arch-stamp: configure-stamp 
	$(MAKE) $(CFLAGS)
	-test $$SKIP_TESTS || $(MAKE) upload-smoke
	pod2man --section=1 --release="Debian Project" --center="Debian GNU/Linux manual" $(CURDIR)/script/pugscc $(CURDIR)/debian/pugscc.1
	touch build-arch-stamp

build-indep: build-indep-stamp
build-indep-stamp: configure-stamp 
	touch build-indep-stamp

clean: clean1 unpatch
clean1:
	dh_testdir
	dh_testroot
	rm -f build-arch-stamp build-indep-stamp configure-stamp install-stamp
	-$(MAKE) realclean
	rm -f debian/pugscc.1
	dh_clean

install: install-indep install-arch
install-stamp:
	dh_testdir
	dh_testroot
	$(MAKE) install DESTDIR=$(CURDIR)/debian/tmp
	dh_installdirs
	dh_install --sourcedir=$(CURDIR)/debian/tmp --list-missing
	touch install-stamp

install-indep: install-stamp
	dh_testdir
	dh_testroot
	cp $(CURDIR)/debian/pugs-modules.lintian-overrides $(CURDIR)/debian/pugs-modules/usr/share/lintian/overrides/pugs-modules
	cp -r $(CURDIR)/docs/* $(CURDIR)/debian/pugs-doc/usr/share/doc/pugs-doc/
	cp util/perl6.vim $(CURDIR)/debian/pugs/usr/share/vim/addons/syntax

install-arch: install-stamp
	dh_testdir
	dh_testroot
	rmdir --ignore-fail-on-non-empty $(CURDIR)/debian/pugs/usr/lib/perl6/5.8/

# Build architecture independant packages using the common target.
binary-indep: build-indep install-indep
	dh_testdir -i
	dh_testroot -i
	dh_installchangelogs -i ChangeLog
	dh_installdocs -i
	dh_installexamples -i examples/*
	find $(CURDIR)/debian/pugs-doc/ -type f -size 0 -exec rm -f {} \;
	dh_installman -i
	dh_link -i
	dh_compress  -i
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

# Build architecture dependant packages using the common target.
binary-arch: build-arch install-arch
	dh_testdir -a
	dh_testroot -a
	dh_installchangelogs -a ChangeLog
	dh_installdocs -a
	dh_installman -a debian/pugscc.1
	dh_link -a
	dh_compress  -a
	dh_fixperms -a
	dh_strip -a
	dh_makeshlibs -a
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

binary: binary-arch binary-indep
.PHONY: build clean binary-indep binary-arch binary install install-indep install-arch configure patch unpatch clean1
